package com.yzy.baselibrary.extention

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.PixelFormat
import android.graphics.drawable.Drawable

/**
 *description: Drawable 的扩展.
 *@date 2018/10/12 11:50.
 *@author: YinZeYu.
 */
fun Drawable.toBitmap(): Bitmap {
  val w = this.intrinsicWidth
  val h = this.intrinsicHeight
  val config: Bitmap.Config? = if (this.opacity != PixelFormat.OPAQUE) {
    Bitmap.Config.ARGB_8888
  } else {
    Bitmap.Config.RGB_565
  }
  val bitmap = Bitmap.createBitmap(w, h, config)
  val canvas = Canvas(bitmap)
  this.setBounds(0, 0, w, h)
  this.draw(canvas)
  return bitmap
}